package com.xiangban.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.dao.*;
import com.xiangban.exception.BizException;
import com.xiangban.model.*;
import com.xiangban.model.dto.AccompanyReceiveStatusDto;
import com.xiangban.model.dto.HospitalDto;
import com.xiangban.model.dto.OrderDto;
import com.xiangban.model.res.ApiResultCode;
import com.xiangban.rabbitMq.CancelOrderSender;
import com.xiangban.service.OrderService;
import com.xiangban.service.PayService;
import com.xiangban.util.CommonUtil;
import com.xiangban.util.PageUtils;
import com.xiangban.util.TimeConstants;
import com.xiangban.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.xiangban.util.CommonUtil.BizEmpty;
import static com.xiangban.util.CommonUtil.getOrderNo;


/**
 * @Author ly
 * @date 2021/11/30 17:17
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    public  static  Integer OneDayTime = 86400000;
    public static Integer twoHourTime=7200000;
    @Autowired
    private OrderRefundMapper orderRefundMapper;
    @Autowired
    private PageUtils pageUtils;
    @Autowired
  private   HospitalMapper hospitalMapper;
    @Autowired
    private  OrderMapper orderMapper;
    @Autowired
    private  RelationMapper relationMapper;
   @Autowired
   private CommonUtil commonUtils;
   @Autowired
   private  HttpServletRequest request;
   @Autowired
   private  UserAccompanyMapper userAccompanyMapper;
   @Autowired
   private OrderCommentMapper commentMapper;
   @Autowired
   private SourceConfigMapper sourceConfigMapper;
   @Autowired
   private CancelOrderSender cancelOrderSender;
   @Autowired
   private OrderWasteMapper orderWasteMapper;
    @Autowired
    private AccompanyAwardOrderMapper accompanyAwardOrderMapper;
    @Autowired
    private AccompanyWalletRecodeMapper accompanyWalletRecodeMapper;
   @Autowired
   private PayService payService;
   @Autowired
   private AccompanyReceiveStatusMapper accompanyReceiveStatusMapper;
    public static  String appointmentTime = " 11:00:00";//当天十一点过后就不能预约
    public static  BigDecimal defaultMoney = new BigDecimal("0.01");//陪诊默认价格
    public static BigDecimal platformBrokerage =new BigDecimal(0);//平台抽成
    @Override
    public Object getOrderInfo() {//
     //uid 和根据时间 获取最新订单
//        String userId = commonUtils.getUserId(request);
        String userId = commonUtils.getUserId(request);
        if (CommonUtils.BizEmpty(userId)){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
         Order order = new Order();
        order.setUserId(Integer.parseInt(userId));
     List<Order> orderList= orderMapper.getOrderInfoByUid(order);
        return orderList;
    }
/**
 * 获取服务对象
 * */
    @Override
    public Object getServiceObject() {
        //获取uid
        String userId = commonUtils.getUserId(request);
        if (BizEmpty(userId)){
            throw  new BizException(ApiResultCode.FAILED_TOKEN);
        }
        //根据uid去查找绑定的家人
        Relation relation = new Relation();
        relation.setUid(Integer.parseInt(userId));
      List<Relation> relationList=  relationMapper.getFamilyByUserId(relation);
        return relationList;
    }

    /**
     * 添加服务对象
     * */
    @Override
    public Object addServiceObject(Relation relation) throws Exception {
        if (BizEmpty(relation.getName())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(relation.getRelation())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(relation.getPhone())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(relation.getIdCard())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(relation.getSex())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        Boolean aBoolean = commonUtils.checkIdCard(relation.getIdCard(), relation.getName());
        if (!aBoolean){
            throw  new BizException(ApiResultCode.ID_CARD_AND_NAME_INCONSISTENT);
        }
        //获取userid 给这个账户添加家人 最多四个
        String userId = commonUtils.getUserId(request);
        //根据userid查询 relation 表
        if (BizEmpty(userId)){
            throw  new BizException(ApiResultCode.FAILED_TOKEN);
        }
        relation.setUid(Integer.parseInt(userId));
        List<Relation> familyByUserId = relationMapper.getFamilyByUserId(relation);
        //超过包括四人 不能添加
        if (familyByUserId.size()>=4){
            throw  new BizException(ApiResultCode.HAS_REACHED_ITS_LIMIT);
        }
        //根据userid进行亲属绑定
        relationMapper.insertFamilyByUserId(relation);
        return "绑定成功";
    }

    /**
     * 获取医院列表，根据经纬度计算距离
     * */
    @Override
    public PageInfo<HospitalDto> getHospitalList(Hospital hospital) {
        log.info(JSON.toJSONString(hospital));
         PageHelper.startPage(pageUtils.getPage().getPageNum(),pageUtils.getPage().getPageSize());
         if (BizEmpty(hospital.getCity())){
             throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
         }
        //获取用户经纬度，计算经纬度再100公里以内的医院
       List<HospitalDto> hospitalslist= hospitalMapper.getHospitalListByDistance(hospital);
        return new PageInfo<>(hospitalslist);
    }




    /**
     * 选择陪诊人
     * */

    @Override
    public Object chooseEmployee(UserAccompany userAccompany) {
        //将所有陪诊人员列出来  并可以筛选
       List<UserAccompany> userAccompaniesList= userAccompanyMapper.selectAccompanyBySex(userAccompany);
        return userAccompaniesList;
    }

    /**
     * 下单（提交预约界面）
     * */
    @Override
    public Object submitOrder(OrderDto orderDto) throws ParseException {

        //非空判断
         notNull(orderDto);

        OrderDto OrderShare = share(orderDto);//门诊 血透 配药共享数据

        String userId = commonUtils.getUserId(request);
        //根据userid 查询 上一单的下单时间
        Order order = new Order();
        order.setUserId(Integer.parseInt(userId));
        Order order1= orderMapper.getOrderNoByUserIdOne(order);
        if (!BizEmpty(order1)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = order1.getCreateTime();
            long createtime = simpleDateFormat.parse(createTime).getTime();
            long nowTime = simpleDateFormat.parse(simpleDateFormat.format(new Date())).getTime();
            if ((nowTime-createtime)<60000){
                throw new BizException(ApiResultCode.ONE_TIME);
            }
        }
        //将数据插入到订单表
        orderMapper.insertOrder(OrderShare);

        cancelOrderSender.sendMessage(orderDto.getOrderNo(), TimeConstants.DEFAULT_DELAYED_TIME);
        return OrderShare;
    }

    private void notNull(OrderDto orderDto) {
        if (BizEmpty(orderDto.getStartTime())){
             throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getType())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getStartLatitude())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getStartLongitude())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getStartAddress())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getPatientId())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getTripType())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getIsDelete())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        if (BizEmpty(orderDto.getHospitalDrugId())){
            throw new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }

    }


    public OrderDto  share (OrderDto orderDto) throws ParseException {

        //服务对象id
        Integer patientId = orderDto.getPatientId();
        //根据服务对象id 查询姓名电话
        Relation relation = new Relation();
        relation.setId(patientId);
      Relation relation1 =   relationMapper.getFamilyByRelationId(relation);
        String relationName = relation1.getName();
        String relationPhone = relation1.getPhone();
        //下单用户id
        String userId = commonUtils.getUserId(request);
        //出发时间
        String startTime = orderDto.getStartTime();
        if (orderDto.getType()==1||orderDto.getType()==2){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
            String format1 = format.format(new Date());
            String[] s1 = format1.split(" ");
            String ymdTime = s1[0];//获取当前时间年月日
            //当前时间
            long DQtime = format.parse(format1).getTime();
            String[] s = format1.split(" ");
            //当天11点的毫秒值
            long time = format.parse(s[0] + appointmentTime).getTime();



            String[]   s2= startTime.split(" ");//

            Date parse ;
            String YMDTime = s2[0];//出发时间年月日
            try {
                parse = format.parse(startTime);
            }catch (Exception e){
                throw new BizException(ApiResultCode.EXAMINE_TIME);
            }

            long departTime =parse.getTime();
            if (ymdTime.equals(YMDTime)){
                if (!(DQtime<departTime)||!(DQtime<time)){
                    throw new BizException(ApiResultCode.COME_AGAIN_TOMORROW);
                }
            }
            if (!(DQtime<departTime)){
                throw new BizException(ApiResultCode.COME_AGAIN_TOMORROW);
            }
        }



        //生成订单编号
        String orderNo = getOrderNo();

        //出发地点
        BigDecimal startLatitude = orderDto.getStartLatitude();//出发纬度
        BigDecimal startLongitude = orderDto.getStartLongitude();//出发经度
        String startAddress = orderDto.getStartAddress();//出发地址
        //出行方式
        Integer tripType = orderDto.getTripType();
        BigDecimal estimate ;//预估金额



        //服务类型
        Integer type = orderDto.getType();//1 门诊 2 血透 3 配药
        Hospital hospitals = new Hospital();
        Integer hospitalDrugId = orderDto.getHospitalDrugId();//就诊医院id
        if (type==1||type==2){


            //根据就诊医院id 和出发经纬度 查询距离
            orderDto.setHospitalDrugId(hospitalDrugId);//医院id
            hospitals.setId(orderDto.getHospitalDrugId());
            hospitals.setLatitude(startLatitude);
            hospitals.setLongitude(startLongitude);
        }


        if (type==3){
            orderDto.setHospitalDrugId(hospitalDrugId);//医院id
            hospitals.setId(orderDto.getHospitalDrugId());
            hospitals.setLatitude(startLatitude);
            hospitals.setLongitude(startLongitude);
            //获取医院配药还是药店配药
            Integer typeDrug = orderDto.getTypeDrug();
            if (typeDrug==1){
            //医院配药
                hospitals.setType(2);
            }if (typeDrug==2){
                 hospitals.setType(3);
            }



        }
        //查询预估金额  paytype 支付方式 和服务类型去找价格

        SourceConfig sourceConfig = new SourceConfig();

        sourceConfig.setType(type);
      SourceConfig money =  sourceConfigMapper.getMoneyByidAndType(sourceConfig);
        BigDecimal money1 = money.getMoney();
        BigDecimal accompanyMoney = money.getAccompanyMoney();
        estimate=money1;
        List<HospitalDto> hospitalListByDistance = hospitalMapper.getHospitalListByDistance(hospitals);//路程距离
        if (hospitalListByDistance.size()<=0){
            throw  new BizException(ApiResultCode.HOSPITAL_NOT);
        }
        HospitalDto hospitalDto = hospitalListByDistance.get(0);
        Integer distanceUm = hospitalDto.getDistanceUm();//距离 m
        String hospitalName  = hospitalDto.getName();
        double distanceKm= distanceUm/1000.00;


        //计费规则 五公里内 按标准价格收取 超过五公里 按照每五公里加20元 不足五公里 按五公里算
        // 超出金额
        BigDecimal OverBudget = new BigDecimal("0.00");

        double sum=0.0;//总里程
        if (distanceKm<=5.00){

        }else {
            sum  = (int)(distanceKm-5)/5;//商
            double v1 = (distanceKm-5) % 5.0;//余数
            //如果余数不 是0  商就加1
            if (v1!=0.0){
                sum = sum + 1;//这是 不满5 公里按五公里算
            }

            //预估金额
            estimate = (BigDecimal.valueOf(sum*20).add(estimate));
            //超出金额
            OverBudget = OverBudget.add(new BigDecimal(sum * 20));

        }
        //陪诊人员id
        String AccompanyName = "";
        Integer accompanyId = 0;
       BigDecimal unitPrice = new BigDecimal("0.00");
        if (!BizEmpty(orderDto.getAccompanyId())){
            UserAccompany userAccompany= orderMapper.getAccompanyById(orderDto);
            if (BizEmpty(userAccompany)){
                throw new BizException(ApiResultCode.DATA_NOT_EXIST);
            }
            accompanyId = orderDto.getAccompanyId();
            //陪诊人姓名
            AccompanyName = userAccompany.getName();
           unitPrice = userAccompany.getUnitPrice();//加价部分
        }
        BigDecimal add = accompanyMoney.add(unitPrice);//陪诊成本
        orderDto.setStartTime(startTime);//出发时间
        orderDto.setOrderNo(orderNo);//订单号
        orderDto.setStartLatitude(startLatitude);//出发纬度;
        orderDto.setStartLongitude(startLongitude);//出发经度
        orderDto.setUserId(Integer.parseInt(userId));//下单用户id
        orderDto.setPatientId(patientId);//患者id
        orderDto.setEstimate(estimate);//预估金额
        orderDto.setMoney(estimate);//金额
        orderDto.setExceed(OverBudget);//超出金额
        orderDto.setDistance(distanceUm);//总里程
        orderDto.setAccompanyId(accompanyId);//陪诊id
        orderDto.setAccompanyName(AccompanyName);//陪诊名字
        orderDto.setHospitalName(hospitalName);//医院名字
        orderDto.setTripType(tripType);//出行方式
        orderDto.setStartAddress(startAddress);//出发地址
        orderDto.setAccompanyStatus(0);//默认状态
        orderDto.setBasic(money1);
        orderDto.setPhone(relationPhone);
        orderDto.setName(relationName);
        orderDto.setAccompanyMoney(add);


        return orderDto;
    }

    public OrderDto send(OrderDto orderDto){
         //根据orderNo查找
        try {
            orderDto= orderMapper.getStatusByOrderNo(orderDto);
        }catch (Exception e){
            System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!");
            System.out.println(e);
        }


        return orderDto;
    }

    public void cancelOrder(String orderId, String s) {
        if ("订单超时,系统自动取消".equals(s)){
            //修改订单状态
            OrderDto orderDto = new OrderDto();

            orderMapper.updateStatusByorderNo(orderDto);
        }
    }
    /**
     * 获取订单列表
     * */
    @Override
    public PageInfo<OrderDto> ListOrder(Order order) {
        OrderDto orderDto = new OrderDto();
        Integer status = order.getStatus();
        orderDto.setUserId(Integer.parseInt(commonUtils.getUserId(request)));
        orderDto.setStatus(status);
        PageHelper.startPage(pageUtils.getPage().getPageNum(), pageUtils.getPage().getPageSize());

        //根据状态查询订单
        List<OrderDto> listOrderByStatus = orderMapper.getListOrderByStatus(orderDto);



        return new PageInfo<>(listOrderByStatus);
    }
/**
 * 根据订单编号去查询订单详情
 * */
    @Override
    public Object orderParticulars(OrderDto orderDto) {
        //根据订单编号去查询详情
       List<OrderDto> orderDtos = orderMapper.getListOrderByStatus(orderDto);
        OrderDto orderDto1 = orderDtos.get(0);
        Integer hospitalDrugId = orderDto1.getHospitalDrugId();
        Hospital hospital = new Hospital();
        hospital.setId(hospitalDrugId);
         HospitalDto  hospitalNameById = hospitalMapper.getHospitalNameById(hospital);
        String name = hospitalNameById.getName();
        orderDto1.setHospitalName(name);
        if (orderDto1.getAccompanyId()!=0){
            //根据accompany_id去查询姓名
            OrderDto orderDto2 =  orderMapper.getAccompanyName(orderDto1);
            orderDto1.setAccompanyName(orderDto2.getName());
            //查询接单时间
            AccompanyReceiveOrder order =   orderMapper.getOrderTime(orderDto);
            if (!BizEmpty(order)){
                orderDto1.setOrderTime(order.getCreationTime());
            }else {
                orderDto1.setOrderTime("");
            }

        }else {
            orderDto1.setAccompanyName("");
            orderDto1.setOrderTime("");
        }
        if (orderDto1.getStatus()==3){
            //服务中
            AccompanyReceiveOrder accompanyReceiveOrder =  new AccompanyReceiveOrder();
            accompanyReceiveOrder.setOrderNo(orderDto.getOrderNo());
            AccompanyReceiveStatusDto accompanyReceiveStatus= accompanyReceiveStatusMapper.getStatusOne(accompanyReceiveOrder);//获取当前服务状态
             List<AccompanyReceiveStatusDto> list = new ArrayList<>();
             list.add(accompanyReceiveStatus);
              orderDto1.setAccompanyReceiveStatus(list);
        }

        return orderDto1;
    }

 /**
  * 订单取消
  * */
    @Override
    public Object orderCancel(OrderDto orderDto) {//传status 20  和orderNo
        //根据订单编号取消订单
        try {
            //根据订单编号查询服务对象
            OrderDto orderByOrderNo = orderMapper.getOrderByOrderNo(orderDto);
            orderDto.setStatus(6);
            orderMapper.updateStatusByorderNo(orderDto);
            //根据就诊对象id 修改 user_nmber
            relationMapper.updateUseNumberMinus(orderByOrderNo);
        }catch (Exception e){
            return false;
        }
        return true;
    }
/**
 * 订单评价
 * */
    @Override
    public Object orderEvaluate(OrderComment orderComment) {
        //用户评价插入
        try {
            commentMapper.insertOrderComment(orderComment);
        }catch (Exception e){
            throw  new BizException(ApiResultCode.DUPLICATE_ORDERNO_COMMENT);
        }

        //修改订单状态 的待评价改为已完成
        OrderDto orderDto = new OrderDto();
        orderDto.setOrderNo(orderComment.getOrderNo());
        orderDto.setStatus(5);

        orderMapper.updateStatusByorderNo(orderDto);
        return true;
    }
/**
 * 订单退款申请
 * */
    @Transactional
    @Override
    public Object refundOrderList(OrderRefund orderRefund) throws Exception {//订单编号 退款金额  退款原因 补充说明  status  1
        if (BizEmpty(orderRefund.getCause()) || BizEmpty(orderRefund.getOrderNo())){
            throw  new BizException(ApiResultCode.COMMON_PARAM_NULL);
        }
        //首先看看此单有没有生成退款
        OrderRefund orderRefundis=  orderRefundMapper.getRefundByOrderNo(orderRefund);
        if (!BizEmpty(orderRefundis)){
            throw new BizException(ApiResultCode.NO_REPETITION);
        }
        //根据订单编号 找到流水号 根据流水号 去退款  （查询此订单是否支付）
        OrderWaste orderWaste = new OrderWaste();
        orderWaste.setOrderNo(orderRefund.getOrderNo());
        orderWaste.setStatus(0);
        OrderWaste orderNoByWasteNumber = orderWasteMapper.getOrderNoByWasteNumber(orderWaste);
        if (BizEmpty(orderNoByWasteNumber)){
            throw  new BizException(ApiResultCode.NO_PAY);
        }
        Order order = new Order();
        order.setOrderNo(orderRefund.getOrderNo());
        OrderRefund orderRefund1 = (OrderRefund) ruleRule(order);//查询此单可退金额
        BigDecimal money = orderRefund1.getMoney();//可退金额
        //
        //根据 订单编号去申请退款
        //生成退款编号
        String refundno = commonUtils.getWasteNumber();
        orderRefund.setRefundNo(refundno);
        orderRefund.setStatus("1");
        orderRefund.setMoney(money);
        if (orderRefund.getRemark()==null || orderRefund.getRemark().equals("")){
            orderRefund.setRemark("无");
        }
        orderRefundMapper.insertOrderRefund(orderRefund);


      String falg=  payService.refundOrder(orderRefund.getOrderNo(),money);
      log.info(falg+">>>>>>>>>>>>>>>>>>>>>>>>>");
        OrderDto orderDto =   new OrderDto();
        orderDto.setOrderNo(orderRefund.getOrderNo());
        if (falg==null&& !falg.equals("SUCCESS")) {


            orderDto.setFailStatus(2);
            orderMapper.updateFailStatus(orderDto);
            throw new BizException(ApiResultCode.WEIXIN_REFUND_ERROR);
        }
 //微信退款成功后  修改钱包记录 上级记录 和退款表  修改order表状态
        orderDto.setFailStatus(3);
         orderMapper.updateFailStatus(orderDto);
        orderMapper.updateStatusByorderNo(orderDto);
        orderRefundMapper.updateOrderRefundStatus(orderRefund);
        AccompanyAwardOrder accompanyAwardOrder =   new AccompanyAwardOrder();
        accompanyAwardOrder.setOrderNo(orderRefund.getOrderNo());
        AccompanyAwardOrder awardOrderByOrderNo = accompanyAwardOrderMapper.getAwardOrderByOrderNo(accompanyAwardOrder);
        AccompanyWalletRecode accompanyWalletRecode = new AccompanyWalletRecode();
        accompanyWalletRecode.setTransactionNo(orderRefund.getOrderNo());
        AccompanyWalletRecode accompanyWalletRecode1=   accompanyWalletRecodeMapper.getWalletRecodeByOrderNo(accompanyWalletRecode);
        if (BizEmpty(awardOrderByOrderNo)){

          if (BizEmpty(accompanyWalletRecode1)){
              return "ok";
          }
            //修改本人钱包记录
            accompanyWalletRecode.setStatus(2);
            accompanyWalletRecodeMapper.updateWalletByTransaction(accompanyWalletRecode);
        }
//修改本人钱包
        accompanyWalletRecode.setStatus(2);
        accompanyWalletRecodeMapper.updateWalletByTransaction(accompanyWalletRecode);
//修改上级钱包
        AccompanyAwardOrder accompanyAwardOrder1 = new AccompanyAwardOrder();
        accompanyAwardOrder1.setOrderNo(orderRefund.getOrderNo());
        accompanyAwardOrder1.setStatus(2);
        accompanyAwardOrderMapper.updateAwardStatus(accompanyAwardOrder1);
        return "ok";
    }

    @Override
    public Object orderOk(OrderDto orderDto) {

        return "ok";
    }

    /**
     * 退款列表
     * */
    @Override
    public PageInfo<OrderDto> refundList() {
         PageHelper.startPage(pageUtils.getPage().getPageNum(),pageUtils.getPage().getPageSize());
           //查询就诊对象  服务类型  手机号  退款金额
        String userId = commonUtils.getUserId(request);
        OrderDto orderDto = new OrderDto();
        orderDto.setUserId(Integer.parseInt(userId));
       List<OrderDto> refundList =  orderMapper.getRefundList(orderDto);
        return new PageInfo<>(refundList);
    }

  /**
   * 退款详情
   * */
    @Override
    public Object refundDetails(OrderRefund orderRefund) {
        //根据退款编号查询退款进度
       OrderRefund orderRefund1 =  orderRefundMapper.getRefundDetails(orderRefund);
        return orderRefund1;
    }
  /**
   * 解除绑定关系人
   * */
    @Override
    public void removeBinding(Relation relation) {//解除的id
        try {
            relationMapper.updateFamily(relation);
        }catch (Exception e){
            log.error(e.toString());
            throw new BizException(ApiResultCode.NO_ERRO);
        }
    }

/**
 * 退款规则
 * */
    @Override
    public Object ruleRule(Order order) throws ParseException {
        //根据订单编号 查询订单
        OrderDto orderByOrderNo = orderMapper.getOrderByOrderNo(order);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long startTime = format.parse(orderByOrderNo.getStartTime()).getTime();//出发时间
        String format1 = format.format(new Date());
        long DQTime = format.parse(format1).getTime();//当前时间
        BigDecimal basic = orderByOrderNo.getBasic();
        BigDecimal serviceMoney = new BigDecimal("0.0") ;

        if ((startTime-DQTime)>=OneDayTime){//出发时间大于 24小时
            //收取20%费用
             serviceMoney = basic.multiply(new BigDecimal("0.8")).setScale(2,BigDecimal.ROUND_HALF_UP);//

        }
        if (((startTime-DQTime)<=OneDayTime)&&(startTime-DQTime)>=twoHourTime){//小于二十四小时 大于两小时扣除50%
             serviceMoney = basic.multiply(new BigDecimal("0.5")).setScale(2,BigDecimal.ROUND_HALF_UP);//扣除50%

        }
        if ((startTime-DQTime)<=twoHourTime){
            serviceMoney= new BigDecimal("0");

        }
        OrderRefund  orderRefund = new OrderRefund();
        orderRefund.setMoney(serviceMoney);
        return orderRefund;
    }

    /**
     * 退款订单生成
     * */
    @Override
    public Object refudAdd(OrderRefund orderRefund) throws ParseException {

        return "ok";
    }

    public String  sss(){
        Order order = new Order();
        order.setOrderNo("21122915423727132680");
        String update = orderMapper.update(order);
        System.out.println(update);
        return update;
    }
}
